package com.aliyun.solution.demo.lingma.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController {

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/date")
    public String date() {
        return "date";
    }

    @PostMapping("/common_elements")
    public ResponseEntity<List<Integer>> common_elements(@RequestBody Map<String, List<Integer>> payload) {
        List<Integer> array1 = payload.get("array1");
        List<Integer> array2 = payload.get("array2");
        Map<Integer, Integer> countMap = new HashMap<>();
        List<Integer> common = new ArrayList<>();
        for (Integer num : array1) {
            countMap.put(num, countMap.getOrDefault(num, 0) + 1);
        }
        for (Integer num : array2) {
            if (countMap.getOrDefault(num, 0) > 0) {
                common.add(num);
                countMap.put(num, countMap.get(num) - 1);
            }
        }

        return ResponseEntity.ok(common);
    }




    @PostMapping("/date_interval")
    @ResponseBody
    public String dateInterval(
            @RequestParam("date1") String date1,
            @RequestParam("date2") String date2) {

        return null;
    }
}